package com.jerry.template.module.mainpage.find.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jerry.template.R;
import com.jerry.template.module.mainpage.find.entity.FindMessage;
import com.jerry.template.views.GlideRoundTransform;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by chenhuan on 2016/5/12.
 */
public class FindListAdapter extends RecyclerView.Adapter<FindListAdapter.MyViewHolder> implements View.OnClickListener{

    private Context context;
    private List<FindMessage.DataBean.CategoriesBean.CategoryListBean> categoryList;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;
    private FindMessage.DataBean.CategoriesBean.CategoryListBean categoryListBean;
    private OnRecyclerViewItemClickListener mItemClickListener = null;

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view ,int position);
    }

    public FindListAdapter(Context context, List<FindMessage.DataBean.CategoriesBean.CategoryListBean> categoryList){
        this.context = context;
        this.categoryList = categoryList;
        inflater = LayoutInflater.from(context);
        imageLoader = ImageLoader.getInstance();
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = inflater.inflate(R.layout.activity_find_item, null);
        view.setOnClickListener(this);

        MyViewHolder holder = new MyViewHolder(view);

        holder.iconImage = (ImageView) view.findViewById(R.id.icon_image);
        holder.name = (TextView) view.findViewById(R.id.name);
        holder.intro = (TextView) view.findViewById(R.id.intro);
        holder.subscribeCount = (TextView) view.findViewById(R.id.subscribe_count);
        holder.totalUpdates = (TextView) view.findViewById(R.id.total_updates);

        return holder;
    }


    @Override
    public void onBindViewHolder(MyViewHolder holder, int position){

        categoryListBean = categoryList.get(position);
        //标题
        holder.name.setText(categoryListBean.getName());
        //简介
        holder.intro.setText(categoryListBean.getIntro());
        //总帖数和总评数
        holder.totalUpdates.setText(categoryListBean.getTotal_updates() + "");
        holder.subscribeCount.setText(categoryListBean.getSubscribe_count() + "");
        //icon
        Glide.with(context).load(categoryListBean.getIcon_url()).transform(new GlideRoundTransform(context, 5)).into(holder.iconImage);

        holder.position = position;
        holder.itemView.setTag(holder.position);
    }

    @Override
    public int getItemCount(){
        return categoryList.size();
    }


    @Override
    public void onClick(View v){
        if (mItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(View itemView){
            super(itemView);
        }

        ImageView iconImage;
        TextView intro;
        TextView subscribeCount;
        TextView totalUpdates;
        TextView name;

        int position;


    }
}

